<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="检验单编号" prop="qcCode">
        <el-input
          v-model="queryParams.qcCode"
          placeholder="请输入检验单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验单类型" prop="qcType">
        <el-select v-model="queryParams.qcType" placeholder="请选择检验单类型" clearable>
          <el-option label="来料" value="来料" />
          <el-option label="出货" value="出货" />
          <el-option label="过程" value="过程" />
          <el-option label="外协" value="外协" />
          <el-option label="返修" value="返修" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联单据编号" prop="sourceDocCode">
        <el-input
          v-model="queryParams.sourceDocCode"
          placeholder="请输入关联单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联单据类型" prop="sourceDocType">
        <el-select v-model="queryParams.sourceDocType" placeholder="请选择关联单据类型" clearable>
          <el-option label="采购" value="采购" />
          <el-option label="销售" value="销售" />
          <el-option label="生产" value="生产" />
          <el-option label="外协" value="外协" />
          <el-option label="返修" value="返修" />
        </el-select>
      </el-form-item>
      <el-form-item label="检测人员" prop="inspector">
        <el-input
          v-model="queryParams.inspector"
          placeholder="请输入检测人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测日期" prop="params">
        <el-date-picker
          clearable
          v-model="queryParams.params"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          placeholder="请选择检测日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <el-card style="border-radius: 10px;margin-top: 15px">
    <el-table v-loading="loading" :data="unqualifiedRecordList" @selection-change="handleSelectionChange" border>
      <el-table-column label="检验单编号" align="center" prop="qcCode" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['qc:unqualifiedRecord:query']"
          >{{ scope.row.qcCode }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="检验单类型" align="center" prop="qcType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.qcType === '来料'" type="success">{{ scope.row.qcType }}</el-tag>
          <el-tag v-else-if="scope.row.qcType === '出货'" type="danger">{{ scope.row.qcType }}</el-tag>
          <el-tag v-else-if="scope.row.qcType === '外协'" type="warning">{{ scope.row.qcType }}</el-tag>
          <el-tag v-else-if="scope.row.qcType === '返修'" type="primary">{{ scope.row.qcType }}</el-tag>
          <el-tag v-else>{{ scope.row.qcType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品物料编码" align="center" prop="itemCode" :show-overflow-tooltip="true"/>
      <el-table-column label="产品物料名称" align="center" prop="itemName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="批次号" align="center" prop="batchCode" :show-overflow-tooltip="true"/>
      <el-table-column label="不良品数量" align="center" prop="unqualifiedNumber">
        <template slot-scope="scope">
          <span :style="{ color: 'red' }">{{ scope.row.unqualifiedNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测人员" align="center" prop="inspector" />
      <el-table-column label="检测日期" align="center" prop="inspectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关联单据编号" align="center" prop="sourceDocCode" :show-overflow-tooltip="true"/>
      <el-table-column label="关联单据类型" align="center" prop="sourceDocType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sourceDocType === '采购'" type="success">{{ scope.row.sourceDocType }}</el-tag>
          <el-tag v-else-if="scope.row.sourceDocType === '销售'" type="danger">{{ scope.row.sourceDocType }}</el-tag>
          <el-tag v-else-if="scope.row.qcType === '外协'" type="warning">{{ scope.row.sourceDocType }}</el-tag>
          <el-tag v-else-if="scope.row.qcType === '返修'" type="primary">{{ scope.row.sourceDocType }}</el-tag>
          <el-tag v-else>{{ scope.row.sourceDocType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="disposeStatus" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['qc:unqualifiedRecord:edit']"
              v-if="scope.row.disposeStatus === '未处理'"
            >处理</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-check"
              @click="handleCheck(scope.row)"
              v-hasPermi="['qc:unqualifiedRecord:check']"
              v-if="scope.row.disposeStatus === '待审批'"
            >审核</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qc:unqualifiedRecord:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改不良品台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="检验单编号" prop="qcCode">
              <el-input v-model="form.qcCode" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验单类型" prop="qcType">
              <el-input v-model="form.qcType" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品物料名称" prop="itemName">
              <el-input v-model="form.itemName" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次号" prop="batchCode">
              <el-input v-model="form.batchCode" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" type="textarea" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="检测人员" prop="inspector">
              <el-input v-model="form.inspector" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测日期" prop="inspectDate">
              <el-date-picker clearable
                              v-model="form.inspectDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择检测日期"
                              disabled
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联单据编号" prop="sourceDocCode">
              <el-input v-model="form.sourceDocCode" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联单据类型" prop="sourceDocType">
              <el-input v-model="form.sourceDocType" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="不良品数量" prop="unqualifiedNumber">
              <el-input v-model="form.unqualifiedNumber" readonly />
            </el-form-item>
          </el-col>
        </el-row>


        <!-- 来料逻辑-->
        <!-- 来料逻辑-->
        <!-- 来料逻辑-->
        <el-row v-if="form.qcType === '来料'||form.qcType === '外协'">
          <el-col :span="8">
            <el-form-item label="让步接收数量" prop="itemRecptNum">
              <el-input-number
                v-model="form.itemRecptNum"
                :min="0"
                :step="0.01"
                :precision="2"
                :max="(form.unqualifiedNumber * 100 - form.scrapNum * 100 - form.chargebackNum * 100) / 100"
                :disabled="optType === 'view' || optType === 'check'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报废数量" prop="scrapNum">
              <el-input-number
                v-model="form.scrapNum"
                :min="0"
                :step="0.01"
                :precision="2"
                :max="(form.unqualifiedNumber * 100 - form.itemRecptNum * 100 - form.chargebackNum * 100) / 100"
                :disabled="optType === 'view' || optType === 'check'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="退回数量" prop="chargebackNum">
              <el-input-number
                v-model="form.chargebackNum"
                :min="0"
                :step="0.01"
                :precision="2"
                :max="(form.unqualifiedNumber * 100 - form.itemRecptNum * 100 - form.scrapNum * 100) / 100"
                :disabled="optType === 'view' || optType === 'check'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" v-if="form.itemRecptNum>0">
            <el-form-item label="让步接收原因" prop="unqualifiedNumber">
              <el-input v-model="form.itemRecptReason"  />
            </el-form-item>
          </el-col>
<!--          <el-col :span="8"></el-col>
          <el-col :span="8"></el-col>-->
        </el-row>

        <!-- 出货逻辑-->
        <!-- 出货逻辑-->
        <!-- 出货逻辑-->
        <el-row  v-if="form.qcType === '出货' || form.qcType === '返修'">
          <el-col :span="12">
            <el-form-item label="返工返修数量" prop="reworkNum">
              <el-input-number v-model="form.reworkNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报废数量" prop="scrapNum">
              <el-input-number v-model="form.scrapNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'"  />
            </el-form-item>
          </el-col>
        </el-row>


        <!-- 过程逻辑-->
        <!-- 过程逻辑-->
        <!-- 过程逻辑-->
        <el-row v-if="form.qcType === '过程'">
          <el-col :span="8">
            <el-form-item label="返工返修数量" prop="reworkNum">
              <el-input-number v-model="form.reworkNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报废数量" prop="scrapNum">
              <el-input-number v-model="form.scrapNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'"  />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="返回上工序数量" prop="repairedNum">
              <el-input-number v-model="form.returnNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />
            </el-form-item>
          </el-col>
        </el-row>


<!--        <el-row>-->
<!--          <el-col :span="12" v-if="form.qcType === '来料'" >-->
<!--            <el-form-item label="让步接收数量" prop="itemRecptNum">-->
<!--              <el-input-number v-model="form.itemRecptNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12" v-else>-->
<!--            <el-form-item label="返工返修数量" prop="reworkNum">-->
<!--              <el-input-number v-model="form.reworkNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="报废数量" prop="scrapNum">-->
<!--              <el-input-number v-model="form.scrapNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'"  />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-row>-->
<!--          <el-col :span="12" v-if="form.qcType === '来料'" >-->
<!--            <el-form-item label="退回数量" prop="chargebackNum">-->
<!--              <el-input-number v-model="form.chargebackNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12" v-if="form.qcType === '过程'">-->
<!--            <el-form-item label="返回上工序数量" prop="repairedNum">-->
<!--              <el-input-number v-model="form.returnNum" :min="0" :step="1" :disabled="optType === 'view' || optType === 'check'" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('未处理')" v-if="optType !== 'view'&& optType !== 'check'">保 存</el-button>
        <el-button type="danger" @click="submitForm('待审批')" v-if="optType !== 'view' && optType !== 'check'">提交审批</el-button>
        <el-button type="success" @click="submitForm('已处理')" v-if="optType === 'check'">通过</el-button>
        <el-button type="danger" @click="submitForm('未处理')" v-if="optType === 'check'">驳回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUnqualifiedRecord, getUnqualifiedRecord, delUnqualifiedRecord, addUnqualifiedRecord, updateUnqualifiedRecord } from "@/api/mes/qc/unqualifiedRecord";

export default {
  name: "UnqualifiedRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 不良品台账表格数据
      unqualifiedRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qcCode: null,
        qcType: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        batchCode: null,
        unqualifiedNumber: null,
        inspector: null,
        inspectDate: null,
        sourceDocCode: null,
        sourceDocType: null,
        params: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      optType: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询不良品台账列表 */
    getList() {
      this.loading = true;
      const query = { ...this.queryParams };
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      listUnqualifiedRecord(query).then(response => {
        this.unqualifiedRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮p
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qcCode: null,
        qcType: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        batchCode: null,
        unqualifiedNumber: null,
        inspector: null,
        inspectDate: null,
        sourceDocCode: null,
        sourceDocType: null,
        reworkNum: null,
        scrapNum: null,
        returnNum: null,
        scrapCode: null,
        disposeStatus: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null,
        itemRecptNum:null,
        itemRecptReason:null,
        chargebackNum:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加不良品台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUnqualifiedRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "不良品处理";
        this.optType = 'edit';
      });
    },
    /** 查看按钮操作 **/
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getUnqualifiedRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "不良品查看";
        this.optType = 'view';
      });
    },
    handleCheck(row){
      this.reset();
      const id = row.id || this.ids
      getUnqualifiedRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "不良品处理审核";
        this.optType = 'check';
      });
    },
    /** 提交按钮 */
    submitForm(disposeStatus) {
      this.form.disposeStatus=disposeStatus;
      let unqualifiedNumber = this.form.unqualifiedNumber;
      if (this.form.qcType === '来料'||this.form.qcType === '外协') {
        const total = Number(
          (this.form.itemRecptNum + this.form.scrapNum + this.form.chargebackNum).toFixed(2)
        );
        const expected = Number(this.form.unqualifiedNumber.toFixed(2));

        if (total !== expected) {
          this.$modal.msgError(`让步接收数量、退回数量、报废数量合计 ${total} 与不良品数量 ${expected} 不相等`);
          return;
        }
      }else if (this.form.qcType === '过程'){
        // 返工返修数量
        let reworkNum = this.form.reworkNum;
        // 报废数量
        let scrapNum = this.form.scrapNum;
        // 返回上工序数量
        let returnNum = this.form.returnNum;
        if (reworkNum + scrapNum + returnNum !== unqualifiedNumber) {
          this.$modal.msgError(" 返工返修数量 + 报废数量 + 返回上工序数量 必须等于不良品数量");
          return;
        }
      }else if (this.form.qcType === '出货' || this.form.qcType === '返修') {
        // 返工返修数量
        let reworkNum = this.form.reworkNum;
        // 报废数量
        let scrapNum = this.form.scrapNum;
        if (reworkNum + scrapNum !== unqualifiedNumber) {
          this.$modal.msgError(" 返工返修数量 + 报废数量 必须等于不良品数量");
          return;
        }
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUnqualifiedRecord(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    /*handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除不良品台账编号为"' + ids + '"的数据项？').then(function() {
        return delUnqualifiedRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },*/
    /** 导出按钮操作 */
    /*handleExport() {
      this.download('qc/unqualifiedRecord/export', {
        ...this.queryParams
      }, `unqualifiedRecord_${new Date().getTime()}.xlsx`)
    }*/
  }
};
</script>
